﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL.Ventas;
using BOL.UI;
using BOL.Cliente;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

namespace UILGestion.VentaAdmin.DocumentoVenta
{
    public partial class NotaCreditoItemGrid : System.Web.UI.UserControl
    {
        private string DOCUMENTOVENTAITEMINFO = "DocumentoVentaItemInfo";

       
        protected void Page_Load(object sender, EventArgs e)
        {

        }
      
        private void AddItem(DAL.OrdenVentaFormato ordenVentaFormato)
        {
            List<DocumentoVentaitemInfo> ds = (List<DocumentoVentaitemInfo>)Session[idColeccion.Value];

            var q = from a in ds
                    where a.idOrdenVentaFormato == ordenVentaFormato.idInternal
                    select a;
            if (q.Count() == 0)
            {
                decimal totalSaldo = ordenVentaFormato.total - ordenVentaFormato.DocumentoVentaItems.Sum(p => p.total);
                if (totalSaldo > 0)
                {

                    DocumentoVentaitemInfo item = new DocumentoVentaitemInfo(
                                                                internalID.Value.ToInt(),
                                                                ordenVentaFormato);

                    ds.Add(item);

                    Session[idColeccion.Value] = ds;

                }
            }
        }
       
        
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);

                List<DocumentoVentaitemInfo> ds =
                (List<DocumentoVentaitemInfo>)Session[idColeccion.Value];

                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());

                string descripcionEditTextBox =
                     ((TextBox)gv.Rows[e.RowIndex].FindControl("descripcionEditTextBox")).Text;
                
                var q = ds.Single(p => p.idInternal == idinternal);

                q.SetDescripcion(descripcionEditTextBox);

                Session[idColeccion.Value] = ds;

                gv.EditIndex = -1;

                BindDataItem();
                itemGridView.FooterRow.Visible = false;

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;
                BindDataItem();
                itemGridView.FooterRow.Visible = false;
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                List<DocumentoVentaitemInfo> ds = (List<DocumentoVentaitemInfo>)Session[idColeccion.Value];


                var drw = ds.Single(p=>p.idInternal==idInternal);

                ds.Remove(drw);
                
                //eRolList.Tables[1].Rows.Remove(drw);

                Session[idColeccion.Value.ToString()] = ds;

                BindDataItem();
                ((DocumentoVentaEditForm)this.Page).SetEstadistica();




            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        
        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<DocumentoVentaitemInfo> itemList0 = new List<DocumentoVentaitemInfo>();
                DocumentoVentaitemInfo itemcero = new DocumentoVentaitemInfo();

                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }
        public void BindDataItem()
        {

            List<DocumentoVentaitemInfo> ds = (List<DocumentoVentaitemInfo>)Session[idColeccion.Value];

            itemGridView.DataSource = ds;
            itemGridView.DataBind();
            resetEmpty();

        }

    }
}